<?php
    class Category_Model extends CI_Model
    {
        var $table = "db_categories";
        public function __construct()
        {
            parent::__construct();
        }
        /***************** Danh muc cha *************/
        public function add_cats_parent($data)
        {
            $data = array(
                'name_cats' => $data['name_cats'],
                'order_cats'=> $data['order_cats'],
                'active'    => $data['active'],
                'id_parent' => $data['id_parent'],
                'url'       => $data['url']               
            );
            $this->db->set($data);
            if($this->db->insert($this->table)) return true; else return false;
        }
        public function update_cats_parent($id,$data)
        {
            $this->db->where('id_cats',$id);
            $data = array(
                'name_cats' => $data['name_cats'],
                'order_cats'=> $data['order_cats'],
                'active'    => $data['active'],
                'id_parent' => $data['id_parent'],
                'url'       => $data['url']                
            );
            $this->db->set($data);
            if($this->db->update($this->table)) return true; else return false;
        }
        public function list_cats_parent()
        {
            return $this->db->get($this->table)->result();
        }
        public function list_cats_children()
        {
            $this->db->where('id_parent !=',0);
            return $this->db->get($this->table)->result();
        }
        public function get_cats_parent($id)
        {
            $this->db->where('id_cats',$id);
            return $this->db->get($this->table)->row();
        }
        function change_update_active($id,$data)
        {
            $this->db->where('id_cats',$id);
            $data = array(
            'active' => $data['active']
            );
            $this->db->set($data);
            if($this->db->update($this->table)) return true; else return false;
        }
        public function delete_cats_parent($id)
        {
            $this->db->where('id_cats',$id);
            return $this->db->delete($this->table);
        }
        public function get_name_parent_id($id)
        {
            $q = $this->db->get_where($this->table,array('id_cats'=>$id));
            if($q->num_rows () > 0)
            {
                return $q->row()->name_cats;
            }
            else
            {
                return false;                
            }
        }
        function total($dk = null)
		{
			if($dk != null)
			{
				$this->db->where('id_cats', $dk);
			}
			
			
			
			return $this->db->count_all_results($this->table);
		}
        function product_phantrang($begin,$max)
		{
			if($begin == ''){
				$begin = 0;
			}
			if($max >= 0 && $max != ''){
				$limit = 'limit '.$begin.','. $max;
			}else{
				$limit ='';	
			}
            
		return $this->db->query('select * from db_categories  order by id_cats '. $limit)->result();	
		}
        /***************** Danh muc con *************/
       
    }
?>